package me.goldze.mvvmhabit.bus;

import java.util.HashMap;

public class SourceUtils {
    public static _NetModel lineMode;
    private static HashMap<String, Object> cache = new HashMap<>();

    public static <T> T getSource(Class<? extends T> local, Class<? extends T> http) {
        switch (lineMode) {
            case OFFLINE:
                return getSource(local);
            default:
                return getSource(http);
        }
    }

    /**
     * 反射获取对象
     *
     * @param source 来源
     * @return {@link T}
     */
    private static <T> T getSource(Class<? extends T> source) {
        T t = (T) cache.get(source.getSimpleName());
        if (t == null) {
            try {
                t = source.newInstance();
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            }
        }
        cache.put(t.getClass().getSimpleName(), t);
        return t;
    }


}
